package vip.mall.modular.orders.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import vip.mall.common.result.CommonResult;
import vip.mall.modular.orders.params.AddOrderParams;
import vip.mall.modular.orders.params.PageOrderParams;
import vip.mall.modular.orders.service.OrdersService;
import vip.mall.modular.orders.vo.OrderTableVo;

import java.util.List;

/**
 * @author Asus
 * @Date 2025/2/10 15:26
 * @Descripition
 */

@RequestMapping("/orders")
@RestController
@Tag(name = "订单业务")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @PostMapping("/add")
    @Operation(summary = "增加订单")
    public CommonResult add(@RequestBody AddOrderParams addOrderParams){
        if(ObjectUtil.isEmpty(addOrderParams)){
            return CommonResult.error("订单信息不能为空");
        }
        boolean result = ordersService.add(addOrderParams);
        return result ? CommonResult.success():CommonResult.error();
    }

    @GetMapping("/delete")
    @Operation(summary = "删除订单")
    public CommonResult delete(@RequestParam("id") String id){
        boolean result = ordersService.removeById(id);
        return result ? CommonResult.success():CommonResult.error();

    }

    @PostMapping("/update")
    @Operation(summary = "修改订单")
    public CommonResult update(@RequestBody OrderTableVo orders){
        ordersService.edit(orders);
        return CommonResult.success("修改订单信息成功");
    }

   @PostMapping("/page")
    @Operation(summary = "分页获取订单列表")
    public CommonResult page(@RequestBody PageOrderParams pageOrderParams){
       System.out.println(pageOrderParams);
        return CommonResult.success(ordersService.pageList(pageOrderParams));
   }
   @GetMapping("/cartGet")
    @Operation(summary = "根据用户ID获取购物车列表")
    public CommonResult cartGet(@RequestParam("userId") String userId){
       return CommonResult.success(ordersService.cartGet(userId));
   }
    @PostMapping("/editCart")
    @Operation(summary = "结算购物车")
    public CommonResult cartGet(@RequestBody List<String> ids){
        ordersService.editCart(ids);
        return CommonResult.success();
    }
}
